﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Tools;
using System.IO;
using System.Web;
using System.Web.Script.Serialization;

namespace ViewDataBusiness
{
    public static class ViewBusiness
    {
        public static ViewSetting LoadViewSetting(string viewForder, string viewName)
        {
            try
            {
                string path = HttpContext.Current.Server.MapPath("/Views/" + viewForder + "/" + viewName + ".json").ToLower();
                string fileText = (string)CacheData.GetCache(path);
                if (fileText == null)
                {
                    if (File.Exists(path))
                    {
                        fileText = File.ReadAllText(path, Encoding.UTF8);
                        CacheData.InsertCache(path, fileText, path);
                    }
                    else
                    {
                        return new ViewSetting();
                    }
                }

                return new JavaScriptSerializer().Deserialize<ViewSetting>(fileText);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        public static void SaveViewSetting(string viewForder, string viewName, ViewSetting viewSetting)
        {
            try
            {
                string path = HttpContext.Current.Server.MapPath("/Views/" + viewForder + "/" + viewName + ".json").ToLower(); 
                string fileText = new JavaScriptSerializer().Serialize(viewSetting);
                File.WriteAllText(path, fileText, Encoding.UTF8);
                CacheData.InsertCache(path, fileText, path);
            }
            catch (Exception e)
            {
                throw (e);
            }
        }

        public static string LoadJsonSetting(string viewForder, string viewName)
        {
            try
            {
                string path = HttpContext.Current.Server.MapPath("/Views/" + viewForder + "/" + viewName + ".json").ToLower();
                string fileText = (string)CacheData.GetCache(path);
                if (fileText == null)
                {
                    if (File.Exists(path))
                    {
                        fileText = File.ReadAllText(path, Encoding.UTF8);
                        CacheData.InsertCache(path, fileText, path);
                    }
                    else
                    {
                        return null;
                    }
                }

                return fileText;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        public static void SaveJsonSetting(string viewForder, string viewName, string jsonSetting)
        {
            try
            {
                string path = HttpContext.Current.Server.MapPath("/Views/" + viewForder + "/" + viewName + ".json").ToLower();
                File.WriteAllText(path, jsonSetting, Encoding.UTF8);
                CacheData.InsertCache(path, jsonSetting, path);
            }
            catch (Exception e)
            {
                throw (e);
            }
        }


    }
}
